use ${newdata}carownership_dataset_bergen, clear


/* Only keep required obs */
keep if $trmgroup != 0 
keep if $trmgroup != .
keep if 	(year >= $firstyearpre  & year <= $lastyearpre) ///
		  | (year >= $firstyearpost & year <= $lastyearpost)

/* setup variables */
capt drop bergen 		  
gen commuters = ($trmgroup == 1 | $trmgroup == 3) 
gen bergen = 	($trmgroup == 1 | $trmgroup == 2)
gen post = (year >= $firstyearpost & year <= $lastyearpost)
gen bergen_commuters = bergen * commuters
gen commuters_post = commuters * post
gen bergen_commuters_post = bergen_commuters * post

/* All time-varying variables based on geography (residence or work location)
   are set to their 2014 values (variation post 2014 is endogenous to treatment) */
foreach var in dist time_work PublicVSCarTime_fam_mean PublicDiffCarTime_fam_mean grk {
	replace `var' = . if year != 2014
	bysort familienr (`var'): replace `var' = `var'[_n-1] if missing(`var')
}

sort familienr year
egen long famid_num = group(familienr)

/* === Regression 1: Only the triple difference ============================= */
eststo clear
capt drop aux
gen aux = 1

reghdfe $yvar ///
	bergen_commuters_post /// DiDiD: post commuter in Bergen
	commuters_post /// post commuter		
	i.bergen##i.post /// bergen/post
	commuters /// commuters
	bergen_commuters /// bergen commuter
	, absorb(aux) /// do not absorb other FE
	vce(cluster grk)	
eststo reg_1
summarize $yvar if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
estadd scalar MeanValue1 = r(mean), replace
summarize $yvar if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
estadd scalar MeanValue2 = r(mean), replace
eststo reg_1 	
estimates save ${ster}TableD4_col1_${yvar}, replace

/* === Regression 2: Year specific trends =================================== */
reghdfe $yvar ///
	bergen_commuters_post /// DiDiD: post commuter in bergen
	bergen##i.year /// bergen time trend
	commuters##i.year /// commuters time trend
	bergen_commuters /// commuters in bergen
	, absorb( i.year) /// time trend
	vce(cluster grk)	
eststo reg_2
summarize $yvar if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
estadd scalar MeanValue1 = r(mean), replace
summarize $yvar if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
estadd scalar MeanValue2 = r(mean), replace
eststo reg_2 	
estimates save ${ster}TableD4_col2_${yvar}, replace

/* === Regression 3: Neighborhood times year fixed effects ================== */
reghdfe $yvar ///
	bergen_commuters_post /// DiDiD: post commuter in bergen
	commuters##i.year /// commuters time trend
	bergen_commuters /// commuters in bergen
	, absorb(i.grk#i.year) /// neighborhood times year
	vce(cluster grk)

eststo reg_3	
summarize $yvar if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
estadd scalar MeanValue1 = r(mean), replace
summarize $yvar if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
estadd scalar MeanValue2 = r(mean), replace
estadd local YearGrkFe = "\checkmark", replace
eststo reg_3 
estimates save ${ster}TableD4_col3_${yvar}, replace

/* === Regression 4: Adding control variables =============================== */	
reghdfe $yvar ///
	bergen_commuters_post /// DiDiD: post commuter in bergen	
	$xvar /// control variables
	i.commuters#i.year /// commuters time trend
	bergen_commuters /// commuters in bergen
	, absorb(i.grk#i.year) /// neighborhood times year
	vce(cluster grk)	
eststo reg_4
summarize $yvar if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
estadd scalar MeanValue1 = r(mean), replace
summarize $yvar if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
estadd scalar MeanValue2 = r(mean), replace
eststo reg_4 	
estimates save ${ster}TableD4_col4_${yvar}, replace

/* === Regression 5: Adding household fixed effects ========================= */
reghdfe $yvar ///
	bergen_commuters_post /// DiDiD: post commuter in bergen		
	$xvar /// control variables
	i.commuters#i.year /// commuters time trend
	bergen_commuters /// commuters in bergen
	, absorb( ///
		i.grk#i.year /// neighborhood times year
		i.famid_num /// household fe
	) vce(cluster grk)		

eststo reg_5
summarize $yvar if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
estadd scalar MeanValue1 = r(mean), replace
summarize $yvar if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
estadd scalar MeanValue2 = r(mean), replace
eststo reg_5 	
estimates save ${ster}TableD4_col5_${yvar}, replace

/* === Visual inspection of results === */
esttab *, keep(bergen_commuters_post) stats(MeanValue1 MeanValue2 N N_clust, ///
	label("Mean depvar $reg1lastyearpre" ///
			"Mean depvar $reg1lastyearpost" ///
			"Obs" "No clusters") ///
		fmt(4 4 0 0)) ///
	star(* 0.10 ** 0.05 *** 0.01)
	
